<?php
class Geo_Country_model extends MY_Model
{
	public
		$table = 'geo_countries',
		$prKey = 'id'
	;
	
	function __construct()
	{
		parent::__construct();

		$this->fields = array(
			'id' 		=> '',
			'name' 		=> T_('Country'),
			'iso_nr' 	=> T_('Country number - ISO'),
			'alpha2' 	=> T_('Code 2 literals'),
			'alpha3' 	=> T_('Code 3 literals'),
			'phone_code'=> T_('Phone code'),
		);

		$this->rules = array(
			'name' 		=> "trim|strip_tags|required|max_length[64]|callback___nameUnique",
			'iso_nr' 	=> "trim|strip_tags|required|integer|callback___isoNumberUnique",
			'alpha2' 	=> "trim|strip_tags|alpha|max_length[2]",
			'alpha3' 	=> "trim|strip_tags|alpha|max_length[3]",
			'phone_code'=> "trim|strip_tags|max_length[8]",
		);
	}
	
	function getSelectOptions($config = null)
	{
		/*
		if (!empty($config['id']))
		{
			$this->db->where('id', $config['id']);
		}
		else if (!empty($config['iso_nr']))
		{
			$this->db->where('iso_nr', $config['iso_nr']);
		}
		*/
		
		$this->db->order_by('name');
		$query = $this->db->get($this->table);
		
		foreach ($query->result() as $row)
		{
			$options[$row->iso_nr] = $row->name;
		}
		
		return $options;
	}

	function hasStates($id_country)
	{
		$sql = 'SELECT COUNT(id) AS num FROM geo_states WHERE id_countries='. $id_country;
		$result = $this->db->query($sql)->row();
		
		return $result->num;
	}	

	function search($config = array())
	{
		$query = $this->db->get($this->geo_country_model->table);

		$order 		= $this->input->get_post('order');
		$direction 	= $this->input->get_post('direction');
		$search 	= $this->input->get_post('search');

		// main sql		
		$sql = 'SELECT *';
		$sql .= ' FROM '.$this->table.' c';
		$sql .= ' WHERE 1 = 1';  
			
		if ($search)
		{
			$sql .= ' AND c.name LIKE ' . $this->db->escape('%' . $search . '%'); 
		}
		
		switch ($order)
		{			
			default:
				$sql .= ' ORDER by c.id';
				$direction = 'DESC';
		}
		
		$sql .= ($direction == 'DESC') ? ' DESC' : ' ASC';
		
		$this->data['list'] = $this->db->query($sql)->result();

		$this->data['search'] = $search; 
	}	
	
	function delete($items_serialized = null)
	{
		$items = explode("&", $items_serialized);
		
		$report = "";
		foreach ($items as $id)
		{	
			/*if ($this->geo_country_model->hasStates($id) > 0)
			{
				$this->log->writeMessage('error', T_('This Country has states. It is impossible to remove this Country.'));
				$report .= ",0"; // don't deleted
			}
			else
			{	*/			
        		//$data = $this->geo_country_model->get($id);
        
				$res = $this->db->delete($this->table, array($this->prKey => $id));	

				if (is_null($res))
				{
					$this->log->writeMessage('error', T_('Error. Country was not removed.'));
					$report .= ",0"; // don't deleted					
				}
				else
				{					
					$res2 = $this->db->delete('geo_states', array('id_countries' => $id));	
					
					$this->log->writeMessage('done', T_('Country was removed.'));
					$report .= ",1"; // deleted				
				}	
			//}
		}

		return substr($report, 1);
	}

	function nameUnique($name)
	{
		$this->db->where('name', $name);
		
		if(!empty($this->id_country))
		{
			$this->db->where('id !=', $this->id_country);	
		}
		
		$query = $this->db->get($this->table);

		if (!$query->num_rows())
		{
			return true;
		}
		
		return false;
	}

	function isoNumberUnique($number)
	{
		$this->db->where('iso_nr', $number);
		
		if(!empty($this->id_country))
		{
			$this->db->where('id !=', $this->id_country);	
		}
		
		$query = $this->db->get($this->table);

		if (!$query->num_rows())
		{
			return true;
		}
		
		return false;
	}
	
	function insert($data)
	{
		if (!empty($data))
		{
			parent::insert($data);

			if ($this->db->insert_id() > 0)
			{
				$this->log->writeMessage('done', T_('Country was added succesfully.'));

				return TRUE;
			}
			else
			{
				$this->log->writeMessage('error', T_('Error! Country was not created.'));
				
				return FALSE;
			}
		}		
	}

	function update($data)
	{
		if (!empty($data))
		{
			$res = parent::update($data);

			$this->log->writeMessage('done', T_('Saved succesfully.'));
			
			return TRUE;			
		}		
	}	
	
}
?>